package com.glc.gao.gavin.rxjavademo.operator;

import android.view.View;

import com.glc.gao.gavin.rxjavademo.R;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

public class IntervalActivity extends BaseActivity {

    private Disposable disposable;

    @Override
    public int getDescribeText() {
        return R.string.describe_interval;
    }

    @Override
    protected int getContextView() {
        return R.layout.activity_interval;
    }

    public void executeInterval(View view) {
        print("开始执行事件：" + getNowTimeStr());
        disposable = Observable.interval(2, 3, TimeUnit.SECONDS).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Long>() {

                    @Override
                    public void accept(Long aLong) throws Exception {
                        print("执行事件：" + getNowTimeStr());
                    }
                });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (disposable != null) {
            disposable.dispose();
        }
    }
}
